﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml;
using System.Drawing;
using HgRemoteContracts;

namespace HgRemote
{
	public class Settings
	{
		private static Settings _current;

		public HgUser User { get; set; }
		public string SelectedRepo { get; set; }
		public FormWindowState WindowState { get; set; }
		public Size WindowSize { get; set; }
		public Point WindowLocation { get; set; }
		public int HorizontalSplitDistance { get; set; }
		public int VerticalSplitDistance { get; set; }

		public Settings()
		{
			HorizontalSplitDistance = -1;
			VerticalSplitDistance = -1;
		}

		private static string FilePath
		{
			get { return Path.Combine(Application.LocalUserAppDataPath, @"..\..\UserSettings.xml"); }
		}

		public static Settings Current
		{
			get
			{
				if (_current == null)
				{
					string path = FilePath;
					if (!File.Exists(path))
						_current = new Settings();
					else
					{
						XmlSerializer xs = new XmlSerializer(typeof(Settings));
						using (XmlTextReader xr = new XmlTextReader(new StreamReader(path)))
						{
							_current = xs.Deserialize(xr) as Settings;
						}
					}
				}
				return _current;
			}
		}

		public void SaveChanges()
		{
			XmlSerializer xs = new XmlSerializer(typeof(Settings));
			XmlWriterSettings xSettings = new XmlWriterSettings();
			xSettings.Encoding = Encoding.UTF8;
			xSettings.CloseOutput = true;
			xSettings.ConformanceLevel = ConformanceLevel.Document;
			xSettings.Indent = true;
			using (XmlWriter xw = XmlTextWriter.Create(FilePath, xSettings))
			{
				xs.Serialize(xw, this);
			}
		}


	}
}
